<?php
namespace app\admin\model;

use think\Model;

class BaseModel extends Model{
	/**
	 * [过滤数据权限]
	 * @param $target_field	string	需要过滤的字段
	 */
	public function dataAuth( $target_field = "create_uid" ){
		$dataAuth = session("data_auth");
		if($dataAuth == "*"){
			return [];
		}else{
			return [$target_field => ['in',$dataAuth]];
		}
	}

	/**
	 * [过滤depart_id]
	 */
	public function departIn($target_field = "depart_id") {
		$departModel = new DepartmentModel();
		$depart_ids = [];
		$type = session('data_auth_type');
		switch ($type) {
			case '0':	// 仅自己
				$depart_ids[] = session('depart_id');
				break;
			case '1':	// 所有
				$depart_ids = "*";
				break;
			case '2':	// 本部门
				$depart_ids[] = session('depart_id');
				break;
			case '3':	// 本部门及子部门
				$depart_ids = $departModel->getSonsByPid(session('depart_id'));
				$depart_ids[] = session('depart_id');
				break;
			default: 	// 默认仅自己
				$depart_ids[] = session('depart_id');
				break;
		}
		if($depart_ids == "*"){
			return [];
		}else{
			return [$target_field => ['in',$depart_ids]];
		}
	}
}
